﻿var options = { items_per_page: 20, link_to: "javascript:;", callback: pageChanged }
var maxentries = 0;
$(function() {
    $.ajax({
        type: "GET",
        url: "../api/access.ashx",
        data: { type: 'roleusersnum', roleid: roleid, r: Math.random() },
        dataType: "html",
        success: function(data) {
            if (data > 0) {
                maxentries = data;
                $("#pager").pagination(maxentries, options);
            }
            else {
                formReady();
            }
        }
    });
    $("#btn_delete").click(function() {
        $(this).attr("disabled", "disabled").after("<span style=\"color:red;\">提交中.....</span>");
        var uids = "";

        $("input:checked").not("#cb_All").each(function() {
            uids += $(this).val() + ",";
        });

        $.ajax({
            type: "POST",
            url: "../api/access.ashx",
            data: { type: 'deleteroleusers', roleid: roleid, uids: uids, r: Math.random() },
            dataType: "html",
            success: function(data) {
                if (data == 1) {

                    maxentries = maxentries - $("input:checked").not("#cb_All").length;

                    $("input:checked").not("#cb_All").each(function() {
                        $(this).parents("tr").remove();
                    });

                    $("#cb_All").attr("checked", "");
                    $("#btn_delete").attr("disabled", "disabled").next("span").remove();
                    
                    if (maxentries <= 0) {
                        $("#pager").empty();
                        return;
                    }

                    $("#pager").pagination(maxentries, options);

                    facebox("<img src=\"" + rootpath + "/images/loading.gif" + "\" />操作已完成！");
                    
                }
            }
        });
    });
});

function pageChanged(index, jq) {
    $.ajax({
        type: "GET",
        url: "../api/access.ashx",
        data: { type: 'roleusers', roleid: roleid, pageindex: index + 1, pagesize: options.items_per_page, r: Math.random() },
        dataType: "json",
        success: function(data) {
            $(".DG tbody").empty();
            $.each(data, function(i, item) {
                $(".DG tbody").append("<tr><td><input type='checkbox' value='" + item.uid + "' /> </td><td>" + item.row + "</td><td>" + item.account + "</td><td>" + item.realname + "</td><td>" + item.nickname + "</td><td>" + item.email + "</td></tr>");                
            });
            BindCheckBoxEvent($("#btn_delete"));
            formReady();
            $(window.parent.document).find("iframe").height($(document).height());
        }
    });
}